package id.co.shuba.spamobile.activities;

import java.io.File;
import java.util.ArrayList;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;

import id.co.shuba.spamobile.App;
import id.co.shuba.spamobile.CustomSessionApplication;
import id.co.shuba.spamobile.LogoutReceiver;
import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.Sessions;
import id.co.shuba.spamobile.data.TransactionLogDB;
import id.co.shuba.spamobile.model.UserInfo;
import id.co.shuba.spamobile.widget.FileAdapter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract.Directory;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class ImportListActivity extends SherlockListActivity {			
	private LogoutReceiver logoutReceiver;

	private void setUpLogoutReceiver() {
		IntentFilter intentFilter = new IntentFilter();
	    intentFilter.addAction("id.co.shuba.spamobile.ACTION_LOGOUT");
	    logoutReceiver = new LogoutReceiver(this);
	    registerReceiver(logoutReceiver, intentFilter);
	}
	
	@Override
	protected void onDestroy() {
		unregisterReceiver(logoutReceiver);
		super.onDestroy();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_import_list);
		setUpLogoutReceiver();
		reload();
	}
	
	@Override
	public void onUserInteraction() {		
		super.onUserInteraction();
		((CustomSessionApplication) getApplication()).touch();
		
		if (App.DEBUG) {			
			Log.d("User Interaction", "User interaction to "+this.toString());			
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_import_list, menu);

		return true;
	}	
	
	private void reload() {
		File sdPat = Environment.getExternalStorageDirectory();
		File importPath = new File(sdPat, "spamobile/import");		
		
		if (!importPath.exists())
			importPath.mkdirs();
		
		FileAdapter adapter = new FileAdapter(this, importPath.listFiles());		
		setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {		 
		final File selection = (File)l.getItemAtPosition(position);
		//Toast.makeText(this, selection.getPath(), Toast.LENGTH_LONG).show();		
						
	    new AlertDialog.Builder(new ContextThemeWrapper(this,
				R.style.Theme_Sherlock_Light_Dialog))	    
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle("Import")
        .setMessage(R.string.confirm)
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            	dialog.dismiss();            	
                
            	Intent intent = new Intent(ImportListActivity.this, UpdateOfflineActivity.class);
            	intent.putExtra("FILE", selection.getPath());
            	startActivity(intent);                       
            }
        })        
        .setNegativeButton(R.string.no, null)
        .show();
	}
	
	public void delete(View v) {
	    final File file = (File)v.getTag(); 	 
	    new AlertDialog.Builder(new ContextThemeWrapper(this,
				R.style.Theme_Sherlock_Light_Dialog))	    
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setTitle(R.string.delete)
        .setMessage(R.string.delete_confirm)
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            	dialog.dismiss();            	
                file.delete();
            	
                reload();                       
            }
        })        
        .setNegativeButton(R.string.no, null)
        .show(); 	
	}
	
}
